API Tutorial-II
In this tutorial we shall look at the various ways to get the handle of a window and hide windows using their handles. We shall use the following API's
GetActiveWindow
Here is the declaration for GetActiveWindow(GAW).
Declare Function GetActiveWindow Lib "user32" () As Long
The GetActiveWindow function retrieves the window handle to the active window attached to the calling thread's message queue.
This might be Greek and Latin for some people .In plain English what this means is that this function will return the handle to the active window created by your application and not the window of any other application. Also this window should be capable of accepting keyboard input.
With this function declared, you can now add code to your application to call the function.
dim lwnhandle as long
Private Sub Command1_Click()
lwnhandle=GetActiveWindow()
End Sub
WindowFromPoint
The Declaration for WindowFromPoint(WFP).
Declare Function WindowFromPoint Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
The WindowFromPoint function retrieves a handle to the window that contains the specified point. This is fairly easy to comprehend. Generally WFP is used in conjunction with another api function GetCursorPos(GCP). GCP gives us the co-ordinates of the mouse pointer at any given moment.
dim lwnhandle as long
Private Sub Command1_Click()
lwnhandle=WindowFromPos(125,225)
End Sub
GetDesktopWindow
The Declaration for GetDesktopWindow API is as follows
Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long
The GetDesktopWindow API returns the handle to the desktop window. All windows which are open at any given time are CHILD windows whose parent is the Desktop Window. The function is used when for example we want a list of all windows currently running .If we GetDesktopWindow along with EnumChildWindow API we will get a list of all currently running windows.dim lwnhandle as long
Private Sub Command1_Click()
lwnhandle=GetDesktopWindow()
End Sub
The GetDesktopWindow does not take any parameters.
ShowWindow
The declaration of ShowWindow is as follows
Function ShowWindow Lib "user32" Alias "ShowWindow"
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
The ShowWindow API
is used to show/hide any window.The parameters which ShowWindow takes are the
handle of the window to show/hide and the command ,0 is for hide and 1 is for
show normal.
dim result as long
Private Sub Command1_Click()
result=ShowWindow(lwnhandle,0)
End Sub
The above piece of code will hide the window whose handle is lwhandle.
In this simple tutorial we have learnt how to get the handle of a window and then hide it.Api functions give us the power to do a lot of things with windows. The no of API functions available to us is enormous. If you have any questions/comments or suggestions send them to venky@venkydude.com .Do visit my code projects page for some cool VB codes.